<?php


// No direct access
defined( '_JEXEC' ) or die( 'Restricted access' );

jimport( 'joomla.application.component.view' );


class ShopViewOrders extends JView
{
	function display($tpl = null)
	{
		$id = JRequest::getInt('id', 0);
		$db = &JFactory::getDBO();
		if (is_array($id)) {
            $id = $id[0];
        }
		$edit = is_numeric($id) &&  $id > 0 ;
		if($edit)
		{
			JToolBarHelper::title(JText::_( 'Заказ номер '.$id ), 'generic.png');
			
			JToolBarHelper::apply('applyOrder');
			JToolBarHelper::save('saveOrder');
		//	JToolBarHelper::save('recalcOrder','Пересчитать');
			JToolBarHelper::cancel('cancelOrder');
			
			$model =& $this->getModel();
			$order = $model->getOrder($id);
			
			preg_match_all('#<articul>(.*)<type>(.*)<count>(.*)<price>(.*)<end>#Uis', $order->content, $content, PREG_PATTERN_ORDER);// разбиваем содержимое корзины .
					
			$k=1;
			$buy_sum = 0;// 
			foreach($content[1] as $key=>$tovar)
			{
				$query = "SELECT * FROM #__shop_prod WHERE articul='".$tovar."'";
				$db->setQuery($query);
				$object = $db->loadObject();
				
				$goods[$k][articul] = $content[1][$key];
				$goods[$k][name] = $object->name;
				$goods[$k][quantity] = $content[3][$key];
				$goods[$k][type] = urldecode($content[2][$key]);
				$goods[$k][price] = $content[4][$key];
				$goods[$k][whole_price] = (int)($object->whole_price*130/100);
				$k++;
				$buy_sum += (int)($object->whole_price*130/100);		
			}
			//echo $buy_sum;
			$this->assignRef('goods',$goods);
			$this->assignRef('buy_sum',$buy_sum);
			$this->assignRef('order',$order);
		}
		else
		{
			
			JToolBarHelper::title(JText::_( 'Список заказов' ), 'generic.png');
			
			JToolBarHelper::editListX('editOrder','Изменить');
			JToolBarHelper::deleteListX('Вы уверены что хотите удалить выбранные заказы ?','delOrders');
			
			
			// Get data from the model
			$orders	= & $this->get('Orders');
			
			$this->assignRef('orders',$orders);
		}
		
		
		$this->assignRef('edit',$edit);
		parent::display($tpl);
	}
	
	
	
}
?>